package cn.neo.hhmq.demo;

import cn.neo.hhmq.model.Message;
import cn.neo.hhmq.client.*;
import com.alibaba.fastjson.JSON;

import java.io.IOException;

public class HHMqDemo {

    public static void main(String[] args) throws IOException {

        long ids = 0;

        String topic = "cn.neo.test";
        HHBroker broker = HHBroker.getDefault();

        HHProducer producer = broker.createProducer();
        HHConsumer<?> consumer = broker.createConsumer(topic);
        consumer.listen(topic,message -> {
            // 处理消息
            System.out.println(" onMessage => " + message);
        });

//        HHConsumer<?> consumer1 = broker.createConsumer(topic);
        for (int i = 0; i < 10; i++) {
            Order order = new Order(ids, "item" + ids, 100 * ids);
            producer.send(topic, new Message<>(ids++, JSON.toJSONString(order), null));
        }

//        for (int i = 0; i < 10; i++) {
//            Message<String> message = (Message<String>) consumer1.recv(topic);
//            System.out.println(message);
//            consumer1.ack(topic, message);
//        }

        while (true) {
            char c = (char) System.in.read();
//            if (c == 'q' || c == 'e'){
//                consumer1.unsub(topic);
//                break;
//            }

            if (c == 'p') {
                Order order = new Order(ids, "item" + ids, 100 * ids);
                producer.send(topic, new Message<>(ids++, JSON.toJSONString(order), null));
                System.out.println(" send ok => " + order);
            }

//            if (c == 'c') {
//                Message<String> message = (Message<String>) consumer1.recv(topic);
//                System.out.println(" poll ok => " + message);
//                consumer1.ack(topic, message);
//            }

            if (c == 'a') {
                for (int i = 0; i < 10; i++) {
                    Order order = new Order(ids, "item" + ids, 100 * ids);
                    producer.send(topic, new Message<>((long) ids++, JSON.toJSONString(order), null));
                }
                System.out.println(" send 10 orders ");
            }
        }
    }
}
